﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YuuzleComic.Common;

public partial class admin_pic_down : System.Web.UI.Page
{
    Maticsoft.Common.WebClient wc = new Maticsoft.Common.WebClient();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request["tpadd"] != null && Request["url"] != null)
            {
                try
                {
                    string url = Request.QueryString["url"];
                    if (!url.Contains("http://"))
                    {
                        if (!url.Contains(".xindm.cn"))
                        {
                            url = "http://" + (Request["server"] != null ? Request["server"] : "mh2") + ".xindm.cn/" + url;
                        }
                        else if (Request["server"] != null)
                        {
                            url = "http://" + Request["server"] + url.Substring(url.IndexOf(".xindm.cn"));
                        }
                    }
                    string tpadd = Request.QueryString["tpadd"];
                    tpadd = HttpUtility.UrlDecode(tpadd);
                    if (url.Contains("/"))
                    {
                        string filename = "/comicdata/" + tpadd + url.Substring(url.LastIndexOf("/"));
                        if (!System.IO.Directory.Exists(Server.MapPath("~/comicdata/" + tpadd)))//如果不存在就创建文件夹
                        {
                            System.IO.Directory.CreateDirectory(Server.MapPath("~/comicdata/" + tpadd));
                        }
                        if (!System.IO.File.Exists(Server.MapPath("~" + filename)))
                        {
                            XindmSession.saveImage(url, Server.MapPath("~" + filename));
                        }
                    }
                    Response.Write("{\"success\":\"true\"}");
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                }
                catch (Exception ex)
                {
                    Response.Write("{\"success\":\"false\",\"error\":\"图片:" + Request["url"] + " " + ex.Message + "\"}");
                    Response.End();
                }
            }

        }
    }
}